package org.jsola.hr.service.impl;

import org.jsola.contract.entity.TemplateVariableDO;
import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IAdditionalVariableDAO;
import org.jsola.hr.dto.AdditionalVariableAddDTO;
import org.jsola.hr.dto.AdditionalVariableUpdateDTO;
import org.jsola.hr.entity.AdditionalVariableDO;
import org.jsola.hr.query.AdditionalVariableQuery;
import org.jsola.hr.service.IAdditionalVariableService;
import org.jsola.hr.vo.AdditionalVariableListVO;
import org.jsola.hr.vo.AdditionalVariableVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 员工自定义信息关联变量
 *
 * @author zhr
 */
@Service("hrAdditionalVariableServiceImpl")
public class AdditionalVariableServiceImpl implements IAdditionalVariableService {

    @Autowired
    private IAdditionalVariableDAO additionalVariableDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public AdditionalVariableVO save(AdditionalVariableAddDTO additionalVariableAddDTO, TokenUser tokenUser) {
        // 转DO
        AdditionalVariableDO additionalVariableDO = additionalVariableAddDTO.to(AdditionalVariableDO.class);
        // 保存
        additionalVariableDO = save(additionalVariableDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return additionalVariableDO.to(AdditionalVariableVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public List<AdditionalVariableVO> batchSave(List<AdditionalVariableAddDTO> additionalVariableAddDTOList, TokenUser tokenUser) {
        if (CollectionUtils.isEmpty(additionalVariableAddDTOList)) {
            return null;
        }
        List<AdditionalVariableDO> additionalVariableDOList = additionalVariableAddDTOList
                .parallelStream()
                .map(additionalVariableAddDTO -> {
                    AdditionalVariableDO additionalVariableDO = additionalVariableAddDTO.to(AdditionalVariableDO.class);
                    additionalVariableDO.preInsert(tokenUser.getUserId());
                    additionalVariableDO.setSiteId(tokenUser.getSiteId());
                    return additionalVariableDO;
                }).collect(Collectors.toList());
        additionalVariableDAO.insertListAndSetId(additionalVariableDOList);
        return additionalVariableDOList
                .parallelStream()
                .map(additionalVariableDO -> additionalVariableDO.to(AdditionalVariableVO.class))
                .collect(Collectors.toList());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(AdditionalVariableUpdateDTO additionalVariableUpdateDTO, TokenUser tokenUser) {
        //转DO
        AdditionalVariableDO additionalVariableDO = additionalVariableUpdateDTO.to(AdditionalVariableDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(additionalVariableDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, Long... additionalVariableIds) {
        return deleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) additionalVariableIds);
    }


    @Override
    public AdditionalVariableVO selectById(Long additionalVariableId, String siteId) {
        AdditionalVariableDO additionalVariableDO = selectDOById(additionalVariableId, siteId);
        if (additionalVariableDO == null) {
            return null;
        }
        return additionalVariableDO.to(AdditionalVariableVO.class);
    }

    @Override
    public List<AdditionalVariableListVO> select(AdditionalVariableQuery additionalVariableQuery, String siteId) {
        List<AdditionalVariableDO> additionalVariableDOList = selectDO(additionalVariableQuery, siteId);
        if (CollectionUtils.isEmpty(additionalVariableDOList)) {
            return additionalVariableDOList == null ? null : new ArrayList<>();
        }
        return additionalVariableDOList.stream()
                .map(additionalVariableDO -> additionalVariableDO.to(AdditionalVariableListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(AdditionalVariableQuery additionalVariableQuery, String siteId) {
        Example example = buildExample(additionalVariableQuery, siteId);
        return additionalVariableDAO.selectCountByExample(example);
    }

    @Override
    public Page<AdditionalVariableListVO> selectPage(AdditionalVariableQuery additionalVariableQuery, String siteId) {
        Example example = buildExample(additionalVariableQuery, siteId);
        Page<AdditionalVariableDO> page = additionalVariableDAO.selectPageByExample(example,
                additionalVariableQuery.getPageNo(),
                additionalVariableQuery.getPageSize());

        return page.to(AdditionalVariableListVO.class);
    }

    @Override
    public AdditionalVariableDO selectDOById(Long additionalVariableId, String siteId) {
        return listById(additionalVariableId, siteId);
    }

    @Override
    public List<AdditionalVariableDO> selectDO(AdditionalVariableQuery additionalVariableQuery, String siteId) {
        Example example = buildExample(additionalVariableQuery, siteId);
        return additionalVariableDAO.selectByExample(example);
    }

    @Override
    public List<AdditionalVariableDO> selectByTemplateId(Long templateId, String siteId) {
        Example example = new Example(AdditionalVariableDO.class);
        example.and()
                .andEqualTo("siteId", siteId)
                .andEqualTo("templateId", templateId);
        return additionalVariableDAO.selectByExample(example);
    }

    @Override
    public List<AdditionalVariableDO> selectByTemplateIds(List<Long> templateIds, String siteId) {
        if (CollectionUtils.isEmpty(templateIds)) {
            return new ArrayList<>();
        }
        Example example = new Example(AdditionalVariableDO.class);
        example.and()
                .andEqualTo("siteId", siteId)
                .andIn("templateId", templateIds);
        return additionalVariableDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example
     *
     * @param additionalVariableQuery 查询参数
     * @param siteId                  所属站点id
     * @return example
     */
    private Example buildExample(AdditionalVariableQuery additionalVariableQuery, String siteId) {
        Example example = new Example(AdditionalVariableDO.class);
        example.and()
                .andEqualTo("siteId", siteId);
        // 排序
        ExampleKit.setExampleOrder(example, additionalVariableQuery.getOrders());
        return example;
    }
}




